package com.easylinkin.linkappapi.common.utils.safeAndQuality;

import com.easylinkin.linkappapi.common.service.AppSendMessageService;
import com.easylinkin.linkappapi.common.utils.message.entity.AppSendMessage;
import com.easylinkin.linkappapi.messagecenter.entity.MessageCenter;
import com.easylinkin.linkappapi.messagecenter.entity.enumType.messageTypeEnum;
import com.easylinkin.linkappapi.messagecenter.service.MessageCenterService;
import com.easylinkin.linkappapi.quality.dto.QualityQuestionInfoDto;
import com.easylinkin.linkappapi.safe.dto.HiddenDangerDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Description 发送消息工具类
 * @Author kan yuanfeng
 * @Date 2023/7/11 15:26
 */
@Component
public class SendMsgUtils {
    @Autowired
    private MessageCenterService messageCenterService;
    @Autowired
    private AppSendMessageService appSendMessageService;

    /**
     * 发送安全消息
     * @param title 标题
     * @param content 内容
     * @param ownerId 所属人id
     * @param hiddenDangerDTO 隐患
     * @param isPushJG 是否推送
     */
    public void saveHiddenMessage(String title, String content, String ownerId, HiddenDangerDTO hiddenDangerDTO, Boolean isPushJG) {
        MessageCenter messageCenter = new MessageCenter();
        messageCenter.setTenantId(hiddenDangerDTO.getTenantId());
        messageCenter.setContent(content);
        messageCenter.setOwnerId(ownerId);
        messageCenter.setType(messageTypeEnum.DANGER.ordinal());
        messageCenter.setSelf(false);
        messageCenter.setStatus(0);
        messageCenter.setTitle(title);
        messageCenter.setCreateTime(new Date());
        messageCenter.setLinkId(hiddenDangerDTO.getId());

        messageCenterService.saveOne(messageCenter);
        if(isPushJG){
            // 手机推送
            appSendMessageService
                    .pushMessageToPhone(messageCenter.getId().toString(), AppSendMessage.model.SECURITY, AppSendMessage.type.MSG, ownerId, title,
                            content);
        }
    }

    /**
     * 发送质量消息
     * @param title 标题
     * @param content 内容
     * @param ownerId 所属人id
     * @param qualityQuestionInfoDto 质量
     * @param isPushJG 是否推送
     */
    public void saveQualityMessage(String title, String content, String ownerId, QualityQuestionInfoDto qualityQuestionInfoDto, Boolean isPushJG) {
        MessageCenter messageCenter = new MessageCenter();
        messageCenter.setTenantId(qualityQuestionInfoDto.getTenantId());
        messageCenter.setContent(content);
        messageCenter.setOwnerId(ownerId);
        messageCenter.setType(messageTypeEnum.QUALITY.ordinal());
        messageCenter.setSelf(false);
        messageCenter.setStatus(0);
        messageCenter.setCreateTime(new Date());
        messageCenter.setTitle(title);
        messageCenter.setLinkId(qualityQuestionInfoDto.getId()+"");

        messageCenterService.saveOne(messageCenter);
        if(isPushJG){
            // 手机推送
            appSendMessageService
                    .pushMessageToPhone(messageCenter.getId().toString(), AppSendMessage.model.QUALITY, AppSendMessage.type.MSG, ownerId, title,
                            content);
        }
    }
}
